package cn.yunhe.controller;

import cn.yunhe.pojo.Permission;
import cn.yunhe.pojo.ResultResponse;
import cn.yunhe.service.IPermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.annotation.Secured;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/permission")
public class PermissionController {

    @Autowired
    IPermissionService permissionService;

    @PreAuthorize("hasRole('ROLE_ADMIN')")
    @RequestMapping("/queryAll")
    public ResultResponse queryAll(){
        List<Permission> permissionList = permissionService.queryAll();
        ResultResponse resultResponse = new ResultResponse();
        resultResponse.setData(permissionList);
        return resultResponse;
    }

    @Secured("ROLE_ADMIN")
    @RequestMapping("/insertPermission")
    public ResultResponse insertPermission(@RequestBody Permission permission){
        permissionService.insertPermission(permission);
        return new ResultResponse();
    }

    @RequestMapping("/deletePermissions")
    public ResultResponse deletePermissions(String[] ids){
        permissionService.deletePermissions(ids);
        return new ResultResponse();
    }

}
